//
//  OosTag.m
//  Oos4ios
//
//  Created by Pablo Gonzalez on 3/23/11.
//

#import <oos4ios/Model/OosTag.h>

@implementation OosTag

@dynamic label;
@dynamic term;

@dynamic users;
@dynamic reviews;

static NSMutableDictionary /* NSString, OosTag */ *tagCache;

static NSString *fetchPredicate = @"term = %@";

+ (void)initialize
{
    tagCache = [[NSMutableDictionary dictionary] retain];
}

+ (OosTag *) tagFromTerm:(NSString *)term
{
    OosTag *tag = nil;
    //@synchronized(tagCache) {
        tag = [tagCache objectForKey:term];
    //}
    
    if (!tag) {
        
        tag = [super objectWithPredicate: fetchPredicate, term];
        if (!tag) {
            tag = [super create];
            tag.term = term;
        }
        
//        NSSet *objects = [super objectsWithPredicate: @"term = %@", term];
//        if ([objects count] > 0) {
//            tag = [objects anyObject];
//        } else {
//            tag = [super create];
//            tag.term = term;
//        }
        if (tag) {
            @synchronized(tagCache) {
                [tagCache setObject:tag forKey:term];
            }
        }
    }
    
	return tag;
}

- (NSString *) description
{
    return self.term;
}

@end
